package org.igeek.config.bean.ch02;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @package org.igeek.config.ch02
 * @Description 通过BeanDefinition自己注册Bean对象
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/11/13 15:48
 */
public class BeanDefinitionTest {

    public static void main(String[] args) {
        //1.获取IOC容器
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
        //2.创建BeanDefinition  直接注册bean的定义  到IOC容器中
        //2.1构建beanDefinition的构建者对象
        AbstractBeanDefinition beanDefinition =
                BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        //2.2设置类的类型
        beanDefinition.setBeanClass(Employee.class);
        //2.3设置类的属性信息  作用域scope   singleton单例  prototype原型(非单例)
        beanDefinition.setScope("singleton");
        //2.4将beanDefinition放入ioc容器中进行注册
        ac.registerBeanDefinition("emp",beanDefinition);
        //2.5手动刷新
        //Exception in thread "main" java.lang.IllegalStateException:
        // org.springframework.context.annotation.AnnotationConfigApplicationContext@61064425 has not been refreshed yet
        ac.refresh();
        //3.获取实例Bean

        Employee emp = ac.getBean("emp", Employee.class);

        //4.使用
        emp.setName("小红");
        emp.setSalary("10000.0");
        System.out.println("emp = " + emp);
    }
}
